package org.darcy.framework.schedule;

import java.io.Serializable;

import org.darcy.entity.sys.SysLog;
import org.darcy.service.sys.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
//@Component
public class LogScheduleTask implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
	private SysLogService SysLogService;

	@Autowired
	private LogQueue logQueue;

	@Scheduled(fixedRate = 1000)
	public void consume() {
		if (logQueue.size() > 0) {
			String logStr = logQueue.take();
			if (logStr != null) {
				SysLog log = JSONUtil.toBean(logStr, SysLog.class);
				SysLogService.insert(log);
			}
			log.debug("LogScheduleTask->consume 保存日志", logStr);
		} else {
			//log.debug("LogScheduleTask->consume 没有日志，5秒后再执行");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException ite) {
				log.error("线程休眠失败", ite);
			}
		}
	}

}
